<!--
 * @Description: 墙布
 * 
 * @Author: Edan<Edan>
 * @Date: 2024-07-09 12:49:19
 * @FilePath: /yuxiuproweb/src/views/goods/qb/index.vue
-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="品号" prop="name">
        <el-input
          v-model="queryParams.namenum"
          placeholder="请输入品号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入品名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营销类别" prop="type">
        <!-- <el-input
          v-model="queryParams.type"
          placeholder="请输入营销类别"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-cascader
          v-model="productType"
          :options="ProductTypeArr"
          :props="defaultProps"
          clearable
          @change="ProductTypeChange"
          @keyup.enter.native="handleQuery"
          style="width: 100%"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="面料状态" prop="fabricType">
        <el-select
          style="width: 100%"
          v-model="queryParams.fabricType"
          placeholder="请选择面料状态"
          clearable
        >
          <el-option :label="'素布'" value="1"></el-option>
          <el-option :label="'绣花'" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上架" prop="isShow">
        <el-select
          style="width: 100%"
          v-model="queryParams.isShow"
          placeholder="是否上架"
          clearable
        >
          <el-option :label="'上架'" :value="1"></el-option>
          <el-option :label="'下架'" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="启用" prop="isShow">
        <el-select
          style="width: 100%"
          v-model="queryParams.isuse"
          placeholder="是否启用"
          clearable
        >
          <el-option :label="'启用'" :value="1"></el-option>
          <el-option :label="'未启用'" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['goods:qb:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['goods:qb:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleIsShow(1)"
          >批量上架</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleIsShow(2)"
          >批量下架</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-circle-check"
          size="mini"
          @click="handleIsUse(1)"
          >批量启用</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-remove-outline"
          size="mini"
          @click="handleIsUse(2)"
          >批量停用</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="qbGoodsList"
      @selection-change="handleSelectionChange"
      :max-height="maxHeight"
    >
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="(i, index) in tableForm.item">
        <el-table-column
          :key="index"
          :prop="i.itemcode"
          :columnKey="i.itemcode"
          :label="i.itemname"
          :align="i.aligntype ? i.aligntype : 'center'"
          :min-width="i.minwidth"
          :width="!!i.width ? i.width : ''"
          :show-overflow-tooltip="i.overflow ? true : false"
          :sortable="i.sortable ? true : false"
          v-if="i.displaymark ? true : false"
        >
          <template slot-scope="scope">
            <div v-if="i.itemcode == 'fabricType'">
              <span v-if="scope.row[i.itemcode] == '1'">素布</span>
              <span v-if="scope.row[i.itemcode] == '2'">绣花</span>
            </div>
            <div v-else-if="i.itemcode == 'isShow'">
              <span v-if="scope.row[i.itemcode] == 1">上架</span>
              <span v-else>下架</span>
            </div>
            <div v-else-if="i.itemcode == 'isuse'">
              <span v-if="scope.row[i.itemcode] == 1">启用</span>
              <span v-else>未启用</span>
            </div>
            <div v-else-if="i.itemcode == 'img'">
              <el-image
                class="goodsImg"
                style="width: 40px; height: 40px"
                :src="scope.row[i.itemcode]"
                :preview-src-list="[scope.row[i.itemcode]]"
              >
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>
            </div>
            <span v-else>{{ scope.row[i.itemcode] }}</span>
          </template>
        </el-table-column>
      </template>
      <el-table-column
        label="操作"
        align="center"
        min-width="130"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:qb:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['goods:qb:remove']"
            style="color: #ff9292"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改高端定制对话框 -->
    <add-view
      v-if="open.show"
      :show.sync="open.show"
      :ProductTypeArr="ProductTypeArr"
      v-bind="open"
      @update="getList"
    ></add-view>

    <!-- app图片维护 -->
    <!-- <image-view
      v-if="image.show"
      :show.sync="image.show"
      v-bind="image"
      @update="getList"
    ></image-view> -->
  </div>
</template>

<script>
import {
  getQbGoodsList,
  delQbGoodsList,
  updateQbGoodsListItem,
} from "@/api/goods/qb";
import AddView from "./mods/add.vue";
// import ImageView from "@/views/app/image/mods/add.vue";
import commonMix from "@/utils/mixins/common";
import { tableList } from "./mods/tablecolums.js";
import request from "@/utils/request";
export default {
  name: "qbGoods",
  mixins: [commonMix],
  components: {
    AddView,
    // ImageView,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 墙布表格数据
      qbGoodsList: [],
      // 是否显示弹出层
      open: {
        show: false,
        type: "add",
        data: null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        namenum: null,
        name: null,
        type: null,
        fabricType: null,
        isuse: 1,
        isShow: 1,
      },
      // app相关
      image: {
        show: false,
        type: "4",
        id: null,
      },
      //
      tableForm: tableList,
      productType: [],
      ProductTypeArr: [],
      defaultProps: {
        children: "children",
        label: "typeName",
        value: "typeName",
      },
      selRow: [],
    };
  },
  created() {
    this.getList();
    this.getProductType(); //获取营销类型
  },
  methods: {
    /** 查询高端定制列表 */
    getList() {
      this.loading = true;
      var queryParams = {
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
        data: {
          name: this.queryParams.name,
          namenum: this.queryParams.namenum,
          fabricType: this.queryParams.fabricType,
          type: this.queryParams.type,
          isuse: this.queryParams.isuse,
          isShow: this.queryParams.isShow,
        },
      };
      getQbGoodsList(queryParams).then((response) => {
        this.qbGoodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.ids = selection.map((item) => item.productId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.selRow = selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = {
        show: true,
        type: "add",
        data: null,
      };
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = {
        show: true,
        type: "edit",
        data: {
          ...row,
        },
      };
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectIds = row.productId || this.ids;
      this.$modal
        .confirm("该操作不可逆，是否确定删除？")
        .then(function () {
          return delQbGoodsList(projectIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    ProductTypeChange(val) {
      this.queryParams.type = "";
      for (var i = 0; i < val.length; i++) {
        this.queryParams.type += val[i] + "/";
      }
      if (val.length) {
        this.queryParams.type = this.queryParams.type.slice(
          0,
          this.queryParams.type.length - 1
        );
      }
    },
    getProductType() {
      this.ProductTypeArr = [];
      request({
        url: "/app/userProductType/getType",
        method: "get",
      }).then((res) => {
        this.ProductTypeArr = this.changeFormat(res.rows);
      });
    },
    //
    changeFormat(data) {
      const result = [];
      if (!Array.isArray(data)) {
        return result;
      }
      data.forEach((item) => {
        delete item.children;
      });
      const map = {};
      data.forEach((item) => {
        map[item.id] = item;
      });
      data.forEach((item) => {
        const parent = map[item.fid];
        if (parent) {
          (parent.children || (parent.children = [])).push(item);
        } else {
          result.push(item);
        }
      });
      return result;
    },
    // 批量操作
    async handleIsShow(val) {
      if (this.selRow.length == 0) {
        this.$message.error("请勾选单据");
        return;
      }
      let promiseList = [];
      for (var i = 0; i < this.selRow.length; i++) {
        var item = this.selRow[i];
        var that = this;
        let promise = new Promise((resolve, reject) => {
          item.isShow = val;
          updateQbGoodsListItem(item)
            .then((res) => {
              resolve(res || (val == 1 ? "上架" : "下架") + "成功");
            })
            .catch((er) => {
              // reject("操作失败");
            });
        });
        promiseList.push(promise);
      }
      await Promise.all(promiseList)
        .then((res) => {
          console.log("等所有异步请求完成后执行", res);
          // 等所有异步请求完成后执行
          this.$modal.msgSuccess(
            res.msg || (val == 1 ? "上架" : "下架") + "成功"
          );
          // this.scheduleVisible = false;
        })
        .catch((er) => {})
        .finally(() => {
          this.getList();
        });
    },
    // 批量操作
    async handleIsUse(val) {
      if (this.selRow.length == 0) {
        this.$message.error("请勾选单据");
        return;
      }
      let promiseList = [];
      for (var i = 0; i < this.selRow.length; i++) {
        var item = this.selRow[i];
        var that = this;
        let promise = new Promise((resolve, reject) => {
          item.isuse = val;
          updateQbGoodsListItem(item)
            .then((res) => {
              resolve(res || (val == 1 ? "启用" : "停用") + "成功");
            })
            .catch((er) => {
              // reject("操作失败");
            });
        });
        promiseList.push(promise);
      }
      await Promise.all(promiseList)
        .then((res) => {
          console.log("等所有异步请求完成后执行", res);
          // 等所有异步请求完成后执行
          this.$modal.msgSuccess(
            res.msg || (val == 1 ? "启用" : "停用") + "成功"
          );
          // this.scheduleVisible = false;
        })
        .catch((er) => {})
        .finally(() => {
          this.getList();
        });
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
}
</style>
